
package com.android.formula.activity;

import java.util.ArrayList;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;

import com.android.formula.R;
import com.android.formula.adapter.NoteSubjectListAdapter;
import com.android.formula.adapter.TBaseAdapter;
import com.android.formula.model.NoteSubject;
import com.android.formula.task.DeleteNoteSubjectDBTask;
import com.android.formula.task.GetNoteSubjectDBTask;
import com.android.formula.task.IDataEventHandler;
import com.android.formula.utils.CommonUtils;
import com.android.formula.utils.Constants;

public class SubjectNoteActivity extends BaseGridViewActivity<NoteSubject> implements
        OnClickListener, OnItemLongClickListener {

    private Button mBtnAddNote;

    private PopupMenu mPopupMenu;

    @Override
    protected int getLayout() {
        return R.layout.activity_note_subject;
    }

    @Override
    public void initView() {
        super.initView();
        getActionBar().setDisplayHomeAsUpEnabled(true);
        mBtnAddNote = (Button) findViewById(R.id.note_subject_btn_add_note);
        mBtnAddNote.setOnClickListener(this);

        mGridView.setOnItemLongClickListener(this);
    }

    @Override
    protected void getData() {
        super.getData();
        getDialogsManager().showProgress(R.string.loading_msg);
        GetNoteSubjectDBTask getNoteSubjectDBTask = new GetNoteSubjectDBTask(this,
                new IDataEventHandler<ArrayList<NoteSubject>>() {

                    @Override
                    public void onNotifyNoData() {
                        getDialogsManager().dismissProgress();
                        showNoData();
                    }

                    @Override
                    public void onNotifyErrorMessage(String title, String message) {
                        getDialogsManager().dismissProgress();
                        showNoData();
                        getDialogsManager().showErrorDialog(R.string.error_title,
                                R.string.error_msg, 0, null);
                    }

                    @Override
                    public void onNotifyData(ArrayList<NoteSubject> data) {
                        getDialogsManager().dismissProgress();
                        mListItemDatas.addAll(data);
                        if (mListItemDatas != null && mListItemDatas.size() > 0) {
                            showData();
                        } else {
                            showNoData();
                        }
                    }
                });
        CommonUtils.executeAsyTask(getNoteSubjectDBTask);
    }

    @Override
    protected void clickOnPos(NoteSubject clickedItem, int pos) {
        Intent i = new Intent(this, TypeFormulaNoteActivity.class);
        Bundle bun = new Bundle();
        bun.putSerializable(Constants.DATA_KEY, clickedItem);
        i.putExtras(bun);
        startActivity(i);
    }

    @Override
    protected TBaseAdapter<NoteSubject> getAdapter() {
        return new NoteSubjectListAdapter(this, mListItemDatas);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.note_subject_btn_add_note:
                Intent i = new Intent(SubjectNoteActivity.this, AddNoteActivity.class);
                startActivity(i);
                break;

            default:
                break;
        }
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        final NoteSubject noteSubject = (NoteSubject) parent.getItemAtPosition(position);
        mPopupMenu = new PopupMenu(SubjectNoteActivity.this, view);
        mPopupMenu.getMenu().add(Menu.NONE, Constants.UPDATE_ITEM_MENU_ID, Menu.NONE,
                R.string.btn_update_text);
        mPopupMenu.getMenu().add(Menu.NONE, Constants.DELETE_ITEM_MENU_ID, Menu.NONE,
                R.string.btn_delete_text);
        mPopupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case Constants.UPDATE_ITEM_MENU_ID:
                        updateSubject(noteSubject);
                        break;
                    case Constants.DELETE_ITEM_MENU_ID:
                        getDialogsManager().showConfirmDialog(R.string.delete_title,
                                R.string.delete_subject_msg, R.string.btn_yes,
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        deleteSubject(noteSubject);
                                        mListItemDatas.remove(noteSubject);
                                        mAdapter.notifyDataSetChanged();
                                        dialog.dismiss();
                                    }
                                }, R.string.btn_no, null, true);
                        break;
                    default:
                        break;
                }
                return false;
            }
        });

        mPopupMenu.show();
        return true;
    }

    private void updateSubject(NoteSubject noteSubject) {
        Intent i = new Intent(this, EditSubjectActivity.class);
        Bundle bun = new Bundle();
        bun.putSerializable(Constants.DATA_KEY, noteSubject);
        i.putExtras(bun);
        startActivity(i);
    }

    private void deleteSubject(NoteSubject noteSubject) {
        new DeleteNoteSubjectDBTask(this, noteSubject, null, null).execute(noteSubject);
    }
}
